﻿using System.IO;
using System.IO.Compression;
using System.Text;

namespace CPlase
{
    public static class Compression
    {
        public static string Zip(string InputString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteArray = encoding.GetBytes(InputString);
            MemoryStream Stream = new MemoryStream();
            GZipStream ZipStream = new GZipStream(Stream,
                System.IO.Compression.CompressionMode.Compress);
            ZipStream.Write(byteArray, 0, byteArray.Length);
            ZipStream.Close();
            byteArray = Stream.ToArray();
            System.Text.StringBuilder Builder = new System.Text.StringBuilder(byteArray.Length);
            foreach (byte item in byteArray)
            {
                Builder.Append((char)item);
            }
            Stream.Close();
            ZipStream.Dispose();
            Stream.Dispose();
            return Builder.ToString();
        }

        public static string UnZip(string InputStream)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] byteArray = new byte[InputStream.Length];
            int indexBA = 0;
            foreach (char item in InputStream.ToCharArray())
            {
                byteArray[indexBA++] = (byte)item;
            }

            System.IO.MemoryStream ZipStream = new System.IO.MemoryStream(byteArray);
            System.IO.Compression.GZipStream UnZipStream = new System.IO.Compression.GZipStream(ZipStream,
                System.IO.Compression.CompressionMode.Decompress);

            StreamReader sr = new StreamReader(UnZipStream);
            string Out = sr.ReadToEnd();

            return Out;
        }
    }
}